package com.song.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.commonutils.JwtUtils;
import com.song.commonutils.ResultObj;
import com.song.commonutils.ordervo.CourseWebVoOrder;
import com.song.eduservice.entity.EduCourse;
import com.song.eduservice.entity.EduTeacher;
import com.song.eduservice.entity.chapter.ChapterVo;
import com.song.eduservice.entity.frontvo.CourseFrontVo;
import com.song.eduservice.entity.frontvo.CourseWebVo;
import com.song.eduservice.service.EduChapterService;
import com.song.eduservice.service.EduCourseService;
import com.song.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author song
 * @data 2020/4/6
 */
@Api(description = "前台课程管理")
@RestController
@RequestMapping("/eduservice/courseFront")
public class CourseFrontController {


    @Autowired
    EduCourseService eduCourseService;

    @Autowired
    private EduChapterService chapterService;


    /**
     * 条件查询带分页查询课程
     *
     * @param page
     * @param limit
     * @param courseFrontVo
     * @return
     */
    @ApiOperation("条件查询带分页查询课程")
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public ResultObj getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                        @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> pageCourse = new Page<>(page, limit);
        Map<String, Object> map = eduCourseService.getCourseFrontList(pageCourse, courseFrontVo);
        //返回分页所有数据
        return ResultObj.ok().data(map);
    }

    /**
     * 课程详情的方法
     *
     * @param courseId
     * @return
     */
    @ApiOperation("课程详情的方法")
    @GetMapping("getFrontCourseInfo/{courseId}")
    public ResultObj getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);

        //根据课程id查询章节和小节

        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);
        return ResultObj.ok().data("courseWebVo", courseWebVo).data("chapterVideoList", chapterVideoList);
    }

    /**
     * 根据课程id查询课程信息
     *
     * @param id
     * @return
     */
    @ApiOperation("根据课程id查询课程信息")
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id) {
        CourseWebVo courseInfo = eduCourseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo, courseWebVoOrder);
        return courseWebVoOrder;
    }
}
